package com.fdb.efp.esb.service.impl.ydxxzf;

import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.constant.ESBClientConstance;
import com.fdb.efp.esb.service.facade.ydxxzf.GeneratorOrderService;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.efp.esb.service.bo.req.ydxxzf.GeneratorOrderReqBean;
import com.fdb.efp.esb.service.bo.resp.ydxxzf.GeneratorOrderRespBean;
import com.fdb.efp.esb.service.client.ESBRequestBuilder;
import com.fdb.efp.esb.service.client.EsbBaseBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service("generatorOrderService")
public class GeneratorOrderServiceImpl implements GeneratorOrderService {
    private static Logger logger = LoggerFactory.getLogger(GeneratorOrderServiceImpl.class);

    @Override
    public GeneratorOrderRespBean applyGeneratorOrder(GeneratorOrderReqBean generatorOrderReqBean) throws ESBException {
        try {
            logger.info("======>调用增加工单信息接口【" + generatorOrderReqBean.getLmtApplySeq() + "_30220017_01】开始<======");
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(generatorOrderReqBean.getLmtApplySeq()).build();
            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(generatorOrderReqBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30220017)
                    .withScene(ESBConstant.SCENE_NO_01)
                    .create()
                    .sendAndReceive();

            GeneratorOrderRespBean generatorOrderRespBean = Objects.isNull(recvBean.getBody(GeneratorOrderRespBean.class))?(new GeneratorOrderRespBean()):(recvBean.getBody(GeneratorOrderRespBean.class));
            generatorOrderRespBean.setRetCd(recvBean.getSysHead().getRetInfArry().get(0).getRetCd());
            generatorOrderRespBean.setRetMsg(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            return generatorOrderRespBean;

        } catch (Exception e) {
            logger.error("调用增加工单信息接口【30220017_01】出现异常：" + e.getMessage());
            throw new ESBException("调用增加工单信息接口【30220017_01】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>调用增加工单信息接口【" + generatorOrderReqBean.getLmtApplySeq() + "_30220017_01】结束<======");
        }

    }
}